<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="datePicker">
          <el-form-item label="时间区间" size="small">
            <el-date-picker type="date" placeholder="选择开始日期" value-format="timestamp"
                            v-model="searchForm.startTime"
            ></el-date-picker>
            <span>-</span>
            <el-date-picker type="date" placeholder="选择结束日期" value-format="timestamp"
                            v-model="searchForm.endTime"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    selectList
  } from '@/api/employeepay'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { Message } from 'element-ui'
  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        if (time) {
          var date = new Date(time)
          return formatDate(date, 'yyyy-MM-dd')
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const amount = {
    props: ['colConfig'],
    mounted() {
      console.log(this.colConfig)
    },
    template: `
  <el-table-column :label="colConfig.label" >
        <el-table-column v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                    <span slot-scope="scope">{{scope.row[item.prop]}}</span>
        </el-table-column>
    </el-table-column>
  `
  }

  const formatter = function(row, column) {
    if (row[column.property]) {
      let obj = {}
      obj = global[`${column.property}List`].find((item) => {
        return item.value === row[column.property] // 筛选出匹配数据
      })
      return obj.label
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'teamName', label: '部门名称', width: '100', fixed: true },
          { prop: 'personName', label: '员工姓名', width: '80', fixed: true },
          { prop: 'idNumber', label: '身份证', width: '150' },
          { prop: 'rank', label: '职级', width: '50', formatter: formatter },
          { prop: 'pay', label: '薪资', width: '80' },
          {
            label: '基础工资', children: [
              { prop: 'basicMoney', label: '金额', width: '80' },
              { prop: 'basicProportion', label: '占比(%)', width: '80' }
            ], component: amount
          },
          {
            label: '绩效工资', children: [
              { prop: 'performanceMoney', label: '金额', width: '80' },
              { prop: 'performanceProportion', label: '占比(%)', width: '80' },
              { prop: 'score', label: '得分', width: '80' },
              { prop: 'coefficient', label: '系数', width: '80' },
              { prop: 'realization', label: '实得', width: '80' }
            ], component: amount
          },
          { prop: 'quarterlyBonus', label: '季度奖金', width: '80' },
          {
            label: '各项补贴', children: [
              { prop: 'trafficSubsidy', label: '交通/餐饮补贴', width: '100' },
              { prop: 'otherSubsidy', label: '其他', width: '80' }
            ], component: amount
          },
          {
            label: '其他扣款', children: [
              { prop: 'late', label: '迟到', width: '50' },
              { prop: 'absence', label: '缺勤', width: '50' },
              { prop: 'workHoliday', label: '事假', width: '50' },
              { prop: 'sickLeave', label: '病假', width: '50' },
              { prop: 'absenteeism', label: '旷工', width: '50' },
              { prop: 'subsidyCost', label: '常规补贴扣款', width: '100' },
              { prop: 'otherSubsidyCost', label: '其他', width: '50' }
            ], component: amount
          },
          {
            label: '代扣', children: [
              { prop: 'socialSecurity', label: '社保', width: '80' },
              { prop: 'providentFund', label: '公积金', width: '80' },
              { prop: 'personTax', label: '个税', width: '80' }
            ], component: amount
          },
          { prop: 'netPayroll', label: '实发', width: '80' },
          { prop: 'moneyTime', label: '时间', width: '100', component: time },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        monthForm: {},
        listLoading: true,
        scheduleLoading: true,
        dialogTitle: '新增人员工资支出',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        scheduleData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {},
        visible: false,
        socialInsuranceBase: '',
        providentFundBase: '',
        restCoefficient: '',
        medicalTreatmentCoefficient: '',
        unemploymentCoefficient: '',
        providentFundCoefficient: '',
        idNumber: '',
        MonthlyWages: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        selectList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          console.log(this.tableData)
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
        .item-inline {
          display: inline-block;
          width: 400px;
        }
      }
    }
    .file {
      display: none;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  .inner-dialog {
    .item-inline {
      display: inline-block;
      width: 200px;
    }
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
